# coding=utf-8
from django.db.models import Q

from sutras import models
from sutras import serializers


def get_consumer_list(phone=None, email=None):
    """
    获取用户列表

    返回：
        users: 用户列表
    """
    condition = Q(is_admin=False)
    if phone:
        condition &= Q(phone=phone)
    if email:
        condition &= Q(email=email)
    total_queryset = models.CustomUser.objects.filter(
        condition).order_by("-id")
    users = serializers.ConsumerList(total_queryset, many=True).data
    return users


def get_consumer_address_list(id):
    """
    获取用户地址列表
    """
    address_objs = models.UserAddress.objects.filter(user_id=id)
    address_list = serializers.ConsumerAddressList(
        address_objs, many=True).data
    return address_list


def get_consumer_print_order_list(
        limit, gt_time=None, uid=None, nickname_like=None):
    """
    获取用户印刷订单列表
    """
    condition = Q()
    if gt_time:
        condition &= Q(create_time__gt=gt_time)
    if uid:
        condition &= Q(user__uid=uid)
    if nickname_like:
        condition &= Q(user__nickname__contains=nickname_like)
    order_objs = models.PrintOrder.objects.filter(condition)[:limit]
    order_list = serializers.ConsumerPrintOrderList(order_objs, many=True).data
    return order_list


def get_consumer_favorite_list(gt_time=None, nickname_like=None):
    """
    获取用户收藏列表
    """
    condition = Q()
    if gt_time:
        condition &= Q(create_time__gt=gt_time)
    if nickname_like:
        condition &= Q(user__nickname__contains=nickname_like)
    favorite_objs = models.UserFavorite.objects.filter(condition)
    favorite_list = serializers.ConsumerFavoriteList(
        favorite_objs, many=True).data
    return favorite_list


def get_consumer_transcription_list(gt_time=None, nickname_like=None):
    """
    获取用户抄写列表
    """
    condition = Q()
    if gt_time:
        condition &= Q(create_time__gt=gt_time)
    if nickname_like:
        condition &= Q(user__nickname__contains=nickname_like)
    transcription_objs = models.UserTranscription.objects.filter(condition)
    transcription_list = serializers.ConsumerTranscriptionList(
        transcription_objs, many=True).data
    return transcription_list


def get_consumer_assistance_list(gt_time=None, nickname_like=None):
    """
    获取用户助抄列表
    """
    condition = Q(assistant_user__isnull=False)
    if gt_time:
        condition &= Q(create_time__gt=gt_time)
    if nickname_like:
        condition &= Q(user__nickname__contains=nickname_like)
    assistance_objs = models.UserPageTranscription.objects.filter(condition)
    assistanct_list = serializers.ConsumerAssistanceList(
        assistance_objs, many=True).data
    return assistanct_list
